package com.example.demo.webStudent;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
    // http://127.0.0.1:8080/sun?name=sunny_boy
    @RequestMapping("/sun")
    @ResponseBody
    public String sum(@RequestParam(name = "sunny_boy ",defaultValue = "sunny_boy")String name){
        return "boy" + name;
    }

    // http://127.0.0.1:8080/student
    @RequestMapping("/student")
    @ResponseBody
    public Student user(){
        Student user = new Student();
        user.setName("蔡佳宇");
        user.setId(2021032259);
        user.setIncomes(1000000000);
        return user;
    }

    // http://127.0.0.1:8080/save_student?name=cjy&id=2021032259&incomes=10000000
    @RequestMapping("save_student")
    @ResponseBody
    public String saveStudent(Student s){
        return "Student will save:name="+s.getName()+", id="+s.getId()+", incomes="+s.getIncomes();
    }

    // http://127.0.0.1:8080/html
    @RequestMapping("/html")
    public String html(){
        return "index.html";
    }
    @ModelAttribute
    public void parseStudent(@RequestParam(name = "name",defaultValue = "蔡佳宇")String name,
                             @RequestParam(name = "id",defaultValue = "2021032259") Integer id,
                             @RequestParam(name = "incomes",defaultValue = "100000000") Integer incomes,Student s){
        s.setName("蔡佳宇");
        s.setId(2021032259);
        s.setIncomes(1000000000);
    }
}
